define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    function getDay(num, str) {
        var today = new Date();
        var nowTime = today.getTime();
        var ms = 24*3600*1000*num;
        today.setTime(parseInt(nowTime + ms));
        var oYear = today.getFullYear();
        var oMoth = (today.getMonth() + 1).toString();
        if (oMoth.length <= 1) oMoth = '0' + oMoth;
        var oDay = today.getDate().toString();
        if (oDay.length <= 1) oDay = '0' + oDay;
        return oYear + str + oMoth + str + oDay;
    }

    var Controller = {
        index: function () {
            require(['bootstrap-datetimepicker'], function () {
                var options = {
                    format: 'YYYY-MM-DD',
                    icons: {
                        time: 'fa fa-clock-o',
                        date: 'fa fa-calendar',
                        up: 'fa fa-chevron-up',
                        down: 'fa fa-chevron-down',
                        previous: 'fa fa-chevron-left',
                        next: 'fa fa-chevron-right',
                        today: 'fa fa-history',
                        clear: 'fa fa-trash',
                        close: 'fa fa-remove'
                    },
                    showTodayButton: true,
                    showClose: true
                };
                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker(options);
            });

            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "关键词名称-搜索";};

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'keywords/index' + location.search,
                    add_url: 'keywords/add',
                    edit_url: 'keywords/edit',
                    del_url: 'keywords/del',
                    multi_url: 'keywords/multi',
                    import_url: 'keywords/import',
                    table: 'keywords',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {field: 'keywords', title: '关键词', operate: 'LIKE'},
                        {
                            field: 'create_time',
                            title: '时间',
                            operate:'RANGE',
                            addclass:'datetimepicker',
                            cellStyle: Controller.api.formatter.css,
                            autocomplete:false,
                            visible: false,
                            // formatter: Table.api.formatter.datetime,
                            defaultValue: (new Date()).toLocaleDateString().split('/').join('-')
                        },
                        {field: 'id', title: '当天（'+getDay(0, '-')+'）',
                            operate: false,
                            formatter:function (value,row) {
                                return row.today_count;
                            }},
                        {field: 'id', title: '昨天（'+getDay(-1, '-')+'）',
                            operate: false,
                            formatter:function (value,row) {
                                return row.yesterday_count;
                            }},
                        {field: 'id', title: '前天（'+getDay(-2, '-')+'）',
                            operate: false,
                            formatter:function (value,row) {
                                return row.before_yesterday_count;
                            }},

                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                css:function(){
                    return{
                        css:{
                            "min-width":"100px !important",
                        }
                    };
                },

            }
        }
    };
    return Controller;
});
